package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.GoodsKindMapper;
import com.shop.pojo.GoodsKind;
import com.shop.service.GoodsKindService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsKindServiceImpl implements GoodsKindService {
    @Autowired
    private GoodsKindMapper goodsKindMapper;

    @Override
    public GoodsKind findGoodsKindName(String goodsKindname) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("kinds_name",goodsKindname);
        return goodsKindMapper.selectOne(queryWrapper);
    }

    @Override
    public void add(GoodsKind goodsKind) {
        goodsKindMapper.insert(goodsKind);
    }

    @Override
    public Page<GoodsKind> pageQuery(Integer pagenum, Integer pageSize, String pageQuery) {
        QueryWrapper<GoodsKind> wrapper = new QueryWrapper<>();
        Page<GoodsKind> page = new Page<>(pagenum,pageSize);
        if (pageQuery!=null&&pageQuery.length()>0){
            wrapper.like("kinds_id",pageQuery).or().like("kinds_name",pageQuery);
            page = goodsKindMapper.selectPage(page,wrapper);
        }else{
            page = goodsKindMapper.selectPage(page,null);
        }
        return page;
    }

    @Override
    public void deleteById(Integer id) {
        long count = goodsKindMapper.findCountByGoodsKindId(id);
        if (count>0){
            new RuntimeException();
        }
        goodsKindMapper.deleteById(id);
    }

    @Override
    public void edit(GoodsKind goodsKind) {
        goodsKindMapper.updateById(goodsKind);
    }

    @Override
    public GoodsKind findById(Integer id) {
        return goodsKindMapper.selectById(id);
    }

    @Override
    public List<GoodsKind> findAll() {
        return goodsKindMapper.selectList(null);
    }
}
